<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Session;
use verify\SendCode;
use app\base\model\User;
class Base extends Controller{
    //用户模型
    protected $user;
    protected $userInfo;
    protected $user_id;
    //验证码模板ID
    protected $tempId;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->tempId  = 434445;
        $session_id=$this->request->post('session_id','');
        if(empty($session_id)){
            $msg=checkMsg(LOGIN_ERROR);
            return ['message'=>$msg];
        }
        $this->userInfo=Session::get($session_id,'api');
        $this->user    = new User();
        $this->user_id =$this->userInfo['uid'];
    }
    public function sendMobileCode($phone,$data,$tempId){
        $Rest = new SendCode();
       return  $Rest->sendTemplateSMS($phone,$data,$tempId);
    }
    public function upload($checkData){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate($checkData)->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $a=$info->getSaveName();
            $imgp= str_replace("\\","/",$a);
            $imgpath='/uploads/'.$imgp;
            $msg['message']=checkMsg(UPLOAD_SUCCESS);
            $msg['path']=$imgpath;
            return $msg;
        }else{
            // 上传失败获取错误信息
          //  echo $file->getError();
            $msg['message']=checkMsg(UPLOAD_FAIL);
            return $msg;
        }
    }
   /**
    *创建用户名
    */
   protected function createUserName($length){
       $str='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       $user_name='';
       for($i=0;$i<$length;$i++){
           $user_name.=$str[mt_rand(0, strlen($str) - 1)];
       }
       $count=$this->user->where(['user_name'=>$user_name])->count();
       if($count>0){
           $this->createUserName($length);
       }else{
           return $user_name;
       }
   }
}